#include <bits/stdc++.h>
using namespace std;

#define MAX 100

typedef struct QNode{
    int data;
    struct QNode *next;
} QNode, *QueuePtr;

// 只设置一个尾指针
typedef struct{
    QueuePtr rear;
} LinkQueue;

void InitQueue(LinkQueue &Q){
    Q.rear = new QNode;
    Q.rear->next = Q.rear;
}

int IsEmpty(LinkQueue Q){
    if(Q.rear == NULL) return 1;
    else return 0;
}

int InitEmpty(LinkQueue &Q){ // 清空队列:除了头节点以外的结点删除
    QNode *p = Q.rear->next->next;
    while(p != Q.rear){
        QNode *s = p->next;
        Q.rear->next->next = s;
        delete p;
        p = s;
    }
    delete Q.rear;
    Q.rear = NULL;
    return 1;
}

void EnQueue(LinkQueue &Q, int e){ // 入队
    QNode *p = new QNode;
    p->data = e;
    p->next = Q.rear->next; // 新节点指向头节点
    Q.rear->next = p;
    Q.rear = p; // 更新尾指针
}

void DeQueue(LinkQueue &Q, int &e){ // 出队
    QNode *p;
    p = Q.rear->next->next;
    e = p->data;
    if(p == Q.rear){ // 唯一的结点出队后要清空
        Q.rear = Q.rear->next; // 尾指针指向头节点
    }else{
        Q.rear->next->next = p->next;
        delete p;
    }
}

void Print(LinkQueue Q, int n){
    QNode *t = Q.rear->next->next;
    for(int i = 0; i < n; i++){
        cout << t->data << " ";
        t = t->next;
    }
    cout << '\n';
}

int main(){
    LinkQueue Q;
    InitQueue(Q);
    int num = 0, n = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> num;
        EnQueue(Q, num);
    }
    char c;
    while(1){
        cin >> c;
        if(c == 'E'){
            cin >> num;
            n += 1;
            EnQueue(Q, num);
            Print(Q, n);
        }else if(c == 'D'){
            n -= 1;
            DeQueue(Q, num);
            cout << num << '\n';
            Print(Q, n);
        }else{
            int zk = InitEmpty(Q);
            if(IsEmpty(Q)) cout << "队列已置空\n";
            break;
        }
    }
    return 0;
}